package {
	import flash.display.Bitmap;
	import flash.display.Sprite;
	import flash.display.StageDisplayState;
	import flash.display.StageScaleMode;
	import flash.events.Event;
	import flash.events.KeyboardEvent;
	import flash.events.MouseEvent;
	import flash.system.Capabilities;
	import flash.ui.Keyboard;

	import mylist.listMain;
	import mylist.logs;

	/**
	 * 中兴甩屏项目，手机端
	 * ...
	 * @author db0@qq.com
	 */
	[SWF(width=720,height=1280,backgroundColor=0)]
	// [SWF(width=720,height=700,backgroundColor=0)]
	public class client extends Sprite {
		[Embed(source="asset/phonefront.jpg")]
		private var MainBg : Class;
		private var bg : Bitmap;
		private var main : listMain = new listMain();
		private var txt : InputBoard = new InputBoard();

		public function client() {
			InputBoard.readConfig();
			if (Capabilities.cpuArchitecture == "x86") {
				stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyboardHanlder);
			} else {
				stage.addEventListener(KeyboardEvent.KEY_DOWN, KeyboardHanlder);
				stage.addEventListener(Event.DEACTIVATE, KeyboardHanlder);
			}
			// stage.displayState = StageDisplayState.FULL_SCREEN_INTERACTIVE;
			stage.scaleMode = StageScaleMode.EXACT_FIT;
			if (bg == null)
				bg = new MainBg();
			addChild(bg);
			// mouseChildren = false;
			stage.addEventListener(MouseEvent.CLICK, clicked);
		}

		private function KeyboardHanlder(e : Event) : void {
			// if(stage.contains(main))return;

			if (e is KeyboardEvent) {
				if (KeyboardEvent(e).keyCode == Keyboard.BACK) {
					// logs.adds(String.fromCharCode(KeyboardEvent(e).charCode));
					// return;
					e.preventDefault();
					stage.addChild(txt);
					txt.y = bg.height / 2 - txt.height / 2;
					txt.x = stage.stageWidth / 2 - txt.width / 2;
				} else {
					return;
				}
			}
		}

		private function clicked(e : MouseEvent) : void {
			if (stage.contains(txt)) return;
			stage.removeEventListener(MouseEvent.CLICK, clicked);
			mask = bg;
			stage.addChild(main);
		}
	}
}
import flash.events.Event;
import flash.display.Sprite;
import flash.events.MouseEvent;
import flash.text.TextField;
import flash.text.TextFieldType;
import flash.net.SharedObject;

import mylist.logs;
import mylist.MySocket;

import flash.text.TextFormat;

class InputBoard extends Sprite {
	private static var inputTxt : TextField = new TextField();
	private static var okBtn : TextField = new TextField();
	private static var cancelBtn : TextField = new TextField();

	public function InputBoard() {
		addChild(inputTxt);
		addChild(okBtn);
		addChild(cancelBtn);
		inputTxt.width = 320;
		inputTxt.height = 240;

		inputTxt.defaultTextFormat = okBtn.defaultTextFormat = cancelBtn.defaultTextFormat = new TextFormat(null, 30);
		// inputTxt.wordWrap = true;
		// inputTxt.text = MySocket.ip + ":" + MySocket.port;
		inputTxt.border = true;
		inputTxt.background = true;

		okBtn.autoSize = cancelBtn.autoSize = "left";
		okBtn.text = "确定";
		cancelBtn.text = "取消";
		okBtn.y = inputTxt.y + inputTxt.height * 1.2;
		cancelBtn.y = okBtn.y;
		cancelBtn.x = 320 - cancelBtn.width;

		inputTxt.type = TextFieldType.INPUT;

		okBtn.selectable = cancelBtn.selectable = false;
		okBtn.background = cancelBtn.background = true;
		okBtn.addEventListener(MouseEvent.CLICK, saveConfig);
		cancelBtn.addEventListener(MouseEvent.CLICK, saveConfig);
		addEventListener(Event.ADDED_TO_STAGE, listener);
	}
	private function listener(e:Event):void
	{
		readConfig();
	}
	

	private function saveConfig(e : MouseEvent) : void {
		switch(e.target) {
			case okBtn:
				inputTxt.text = inputTxt.text.replace(/[\r\n\s]/gm, "");
				var arr : Array = inputTxt.text.split(":");
				if (arr) {
					if (uint(arr[1]) > 0 && uint(arr[1] < 65536)) {
						MySocket.port = uint(arr[1]);
					}
					if (String(arr[0]).match(/^(\d{1,3}\.){3}(\d{1,3})$/)) {
						MySocket.ip = String(arr[0]);
					}
				}
				writeConfig(MySocket.ip, MySocket.port);
			case cancelBtn:
				if (stage) stage.removeChild(this);
				break;
			default:
		}
	}

	public static function readConfig() : String {
		var data : String = SharedObject.getLocal("XXX").data["strings"];
		if (data) {
			var xml : XML = XML(data);
			MySocket.ip = String(xml.ip);
			MySocket.port = uint(xml.port);
		}
		inputTxt.text = MySocket.ip + ":" + MySocket.port;
		// inputTxt.type = TextFieldType.INPUT;

		logs.adds(data, MySocket.ip, MySocket.port);
		InputBoard.writeConfig(MySocket.ip, MySocket.port);
		return data;
	}

	public static function writeConfig(ip : String, port : uint) : void {
		var sharedObject : Object = SharedObject.getLocal("XXX");
		sharedObject.data["strings"] = "<data><ip>" + ip + "</ip><port>" + port + "</port></data>";
		sharedObject.flush();
	}
}